package com.gsm.service.impl;

import com.gsm.entity.RotationChart;
import com.gsm.dao.RotationChartDao;
import com.gsm.service.RotationChartService;
import com.gsm.entity.Result;
import com.gsm.utils.IdUtils;
import com.gsm.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 轮播图(RotationChart)表服务实现类
 */
@Service
public class RotationChartServiceImpl implements RotationChartService {
    @Autowired
    private RotationChartDao rotationChartDao;
    @Autowired
    private IdUtils idUtils;


    @Override
    public Result insertRotationChart(RotationChart rotationChart) {
        long chartId = idUtils.nextId();
        int showOrder = 0;
        RotationChart tmpChart = rotationChartDao.selectRotationChartMaxShowOrder();
        if (tmpChart != null) {
            showOrder = tmpChart.getShowOrder() + 1;
        }
        RotationChart chart = RotationChart.builder()
                .chartId(chartId)
                .chartUrl(rotationChart.getChartUrl())
                .webUrl(rotationChart.getWebUrl())
                .status(1)
                .showOrder(showOrder)
                .build();
        rotationChartDao.insertRotationChart(chart);
        Result result = new Result(true, StatusCode.OK);
        return result;
    }

    /**
     * 根据 主键 chartId 删除
     *
     * @param chartId 主键
     * @return
     */
    @Override
    public Result deleteRotationChartByChartId(Long chartId) {
        rotationChartDao.deleteRotationChartByChartId(chartId);
        Result result = new Result(true, StatusCode.OK);
        return result;
    }

    /**
     * 修改
     *
     * @param rotationChart 实例对象
     * @return
     */
    @Override
    public Result updateRotationChartByChartId(RotationChart rotationChart) {
        rotationChartDao.updateRotationChartByChartId(rotationChart);
        Result result = new Result(true, StatusCode.OK);
        return result;
    }

    @Override
    public Result updateRotationCharts(List<RotationChart> charts) {
        rotationChartDao.updateRotationCharts(charts);
        Result result = new Result(true, StatusCode.OK);
        return result;
    }

    /**
     * 根据 id 查询单条数据
     *
     * @param chartId 主键
     * @return
     */
    @Override
    public Result selectRotationChartByChartId(Long chartId) {
        RotationChart rotationChart = rotationChartDao.selectRotationChartByChartId(chartId);
        Result result = new Result(true, StatusCode.OK, "", rotationChart);
        return result;
    }

    /**
     * 根据条件查询列表
     *
     * @param rotationChart 实例对象
     * @return
     */
    @Override
    public Result selectRotationChartList(RotationChart rotationChart) {
        List<RotationChart> list = rotationChartDao.selectRotationChartList(rotationChart);
        Result result = new Result(true, StatusCode.OK, "", list);
        return result;
    }
}